﻿

//动态加载
function response(result,childDiv){
	//alert("result = " + result);
	//alert("childDiv = " + childDiv);
	var innerHTML = childDiv.innerHTML.trim();
	childDiv.innerHTML = innerHTML + result;
}
				
//实例化的对象名称必须与id一样
//性能优化：将图片放到缓存中，不要每次都去服务器读取
component.tree.Tree = function(treeObj,context) {
/*
	var _openDialogObj=null;
	var arr=window.dialogArguments;
	if(arr)
		_openDialogObj=arr[1];
*/
	this.treeObj = treeObj;
	this.context = context;
	this.action = this.context + "/component.do?component=tree";
	
	//下面的常量可以从服务器端设置值
	this.DIV_ID_PREX = "div";
	this.CHECKBOX_NAME_PREX = "child";
	this.IMAGE1_ID_PREX = "image1";
	this.IMAGE2_ID_PREX = "image2";
	this.SPLIT = "_";
	this.PATH_SPLIT = ",";

	this.childDivName = "allChildDiv";
	this.childDivIdArr = new Array();//存放子div，在后台就保存进去，用于显示或隐藏所有
	//this.selectObjArr = new Array();
	this.selectObjMap = new common.Map();//保存多选的数据
	this.objMap = new common.Map();//保存单选的数据
	
	this.map = new common.Map();//存放对应图片
	
	this.map.put("plus.gif","minus.gif");
	this.map.put("minus.gif","plus.gif");
	
	this.map.put("folder.gif","folderopen.gif");
	this.map.put("folderopen.gif","folder.gif");
	
	this.map.put("plusbottom.gif","minusbottom.gif");
	this.map.put("minusbottom.gif","plusbottom.gif");
	
	this.map.put("nolines_plus.gif","nolines_minus.gif");
	this.map.put("nolines_minus.gif","nolines_plus.gif");
	
	//tree的属性,由其他地方注入
	this.treeMap = new common.Map();
	this.treeMap.put("isLoadAll",true);
	this.paras = "";
	/*
	this.condition =_openDialogObj?_openDialogObj.condition:"";
	if(this.condition==null||this.condition=="null"){
		this.condition="";
	}
	*/

	this.xmlHttp = new util.XmlHttpUtil();
	this.formUtil = null;//common.ObjectFactory.getInstance().getFormUtil();

	this.isOpen = true;//展开或收起的标志
}

component.tree.Tree.prototype = {
	isNull:function(value){
		if(value==null || value=="" || value=="undefined"){
			return true;
		}
		return false;
	},
	//获取相反的图片
	getImgByPath:function(imgPath){
		var firstIndex = imgPath.indexOf(this.context);
		var lastIndex = imgPath.lastIndexOf("/");
		var imgName = imgPath.substr(lastIndex+1);
		var imgName2 = this.map.get(imgName);
		var imgPath2 = this.context + "/component/tree/images/" + imgName2;
		return imgPath2;
	},
	changeState:function(divId,isOpen){
		var img1Id = this.IMAGE1_ID_PREX + this.SPLIT + divId.split(this.SPLIT)[1];
		var img1Obj = document.getElementById(img1Id);
		
		var img2Id = this.IMAGE2_ID_PREX + this.SPLIT + divId.split(this.SPLIT)[1];
		var img2Obj = document.getElementById(img2Id);
		
		var childDiv = document.getElementById(divId);
		if(isOpen){
			childDiv.style.display = "block";//显示子节点
			img1Obj.src = this.getImgByPath(img1Obj.src);//改变图片的状态
			if(img2Obj!=null) img2Obj.src = this.getImgByPath(img2Obj.src);
			childDiv.isOpen = true;//设置标志
			var isLoadAll = this.treeMap.get("isLoadAll");
			if(isLoadAll==true || isLoadAll=="true") return;
			var len = childDiv.childNodes.length;
			var childDivHtml = childDiv.innerHTML;
			if(len<2 && !(childDivHtml.indexOf("<div")!=-1 || childDivHtml.indexOf("<DIV")!=-1 )){//动态加载子节点的数据
				var divObj = img1Obj.parentNode;
				var divValue = divObj.value;
				var url = this.action + "&action=doLoadChildNode&divValue="+divValue + "&condition="+this.condition+this.paras;
				this.xmlHttp.sendData(url,this.xmlHttp.SEND_GET,"response",childDiv);//get方式
				//this.xmlHttp.sendData(url,this.xmlHttp.SEND_POST,childDiv,divValue);//post方式
			}
		}else{
			childDiv.style.display = "none";//隐藏子节点
			img1Obj.src = this.getImgByPath(img1Obj.src);//改变图片的状态
			if(img2Obj!=null) img2Obj.src = this.getImgByPath(img2Obj.src);
			childDiv.isOpen = false;//设置标志
		}
	},
	//点击展开或缩回的图片
	onChangeState:function(divId){		
		var childDiv = document.getElementById(divId);
		if(childDiv==null) return false;//子节点就为空
		if(childDiv.isOpen == false || childDiv.isOpen == 'false'){//隐藏就显示
			this.changeState(divId,true);
		}else if(childDiv.isOpen == true || childDiv.isOpen == 'true'){//显示就隐藏
			this.changeState(divId,false);
		}
	},
	//点击节点
	onClickNode:function(divId) {
		var checkboxId = divId.split(this.SPLIT)[1];;
		var checkboxObj = document.getElementById(checkboxId);
		if(!this.isNull(checkboxObj)){
			if(checkboxObj.checked==false){
				checkboxObj.checked = true;
				this.onNodeSelect(checkboxObj);
			}
		}else{//单选
			var img1Id = this.IMAGE1_ID_PREX + this.SPLIT + divId.split(this.SPLIT)[1];
			var img1Obj = document.getElementById(img1Id);
			var divObj = img1Obj.parentNode;
			//alert(divObj.value);

			var treeValueArr = divObj.value.split(";");
			var tempMap = new common.Map();
			tempMap.put("id",treeValueArr[0]);
			tempMap.put("parentId",treeValueArr[1]);
			tempMap.put("name",treeValueArr[2]);
			tempMap.put("type",treeValueArr[3]);
			tempMap.put("level",treeValueArr[4]);
			tempMap.put("path",treeValueArr[5]);
			tempMap.put("otherValue",treeValueArr[7]);
			this.objMap = tempMap;
		}
		
		try{
			eval(this.treeObj + "_onClick(divId)");//扩展接口
		}catch(e){}
		
	},
	//双击节点
	onDblClickNode:function(divId) {
		this.onClickNode(divId);
		
		try{
			eval(this.treeObj + "_onDblClick(divId)");//扩展接口
		}catch(e){}
		
		this.doOK();
	},
	//点击CheckBox:选择某个节点的同时必须选择其所有父节点和所有子节点
	onNodeSelect:function(checkboxObj){
		//alert("checked = " + checkboxObj.checked + ",selectId = " + checkboxObj.value);
		this.onNodeSelect2(checkboxObj);
		try{
			eval(this.treeObj + "_onNodeSelect(checkboxObj)");//扩展接口
		}catch(e){}
		
	},
	onNodeSelect2:function(checkboxObj){
		this.saveTreeId(checkboxObj);
		var id = checkboxObj.value;
		
		var path = checkboxObj.path;
		var pathArr = path.split(this.PATH_SPLIT);
		
		var name = this.CHECKBOX_NAME_PREX + this.SPLIT + id;
		var childArr = document.getElementsByName(name);
		if(checkboxObj.checked==true){
			//选择所有父节点
			for(var i=1;i<pathArr.length-1;i++){
				var parentCheckBox = document.getElementById(pathArr[i]);
				if(parentCheckBox && parentCheckBox.checked==false) {
					parentCheckBox.checked = true;
					this.saveTreeId(parentCheckBox);
				}
			}
			//选择所有子节点
			for(var i=0;i<childArr.length;i++){
				var childCheckBox = childArr[i];
				if(childCheckBox!=null && childCheckBox.checked==false){
					childCheckBox.checked = true;
					this.saveTreeId(childCheckBox);
					this.onNodeSelect2(childCheckBox);//递归选择子节点
				}
			}
		}else{
			//取消选择所有子节点
			this.unSelectChildren(id);
		}
		
	},
	//取消选择所有子节点
	unSelectChildren:function(parentId){
		var name = this.CHECKBOX_NAME_PREX + this.SPLIT + parentId;
		var childArr = document.getElementsByName(name);
		for(var i=0;i<childArr.length;i++){
			var childCheckBox = childArr[i];
			if(childCheckBox!=null && childCheckBox.checked==true){
				childCheckBox.checked = false;
				this.saveTreeId(childCheckBox);
				this.onNodeSelect(childCheckBox);//递归取消选择子节点
			}
		}
	},
	//根据checkbox的id选择节点
	selectCheckboxByIds:function(ids){
		if(!this.isNull(ids)){
			var idArr = ids.split(",");
			for(var i=0;i<idArr.length;i++){
				var checkboxObj = document.getElementById(idArr[i]);
				if(checkboxObj!=null){
					checkboxObj.checked = true;
					this.saveTreeId(checkboxObj);
				}
			}
		}
	},
	saveTreeId:function(checkboxObj){
		if(checkboxObj.value==0 || checkboxObj.value=="0") return;
		if(checkboxObj.checked==true){
			//this.selectObjArr.push(checkboxObj);
			this.selectObjMap.put(checkboxObj.value,checkboxObj);
		}else{
			//this.selectObjArr.remove(checkboxObj);
			this.selectObjMap.put(checkboxObj.value,null);
		}
	},
	getSelectIds:function(){
		var map = this.selectObjMap.map;
		var selectIds = "";
		for(var key in map){
			if(map[key]!=null && key!="extend"){
				selectIds += key + ",";
			}
		}
		if(selectIds!="") selectIds = selectIds.substring(0,selectIds.length-1);
		return selectIds;
	},
	//格式:id,parentId,name,type,level,path
	getSelectTreeArr:function(){
		var map = this.selectObjMap.map;
		var selectTreeArr = new Array();
		for(var key in map){
			if(map[key]!=null && key!="extend"){
				selectTreeArr.push(map[key].parentNode.value);
			}
		}
		return selectTreeArr;
	},
	//格式:id,parentId,name,type,level,path
	getSelectTreeMap:function(){
		var map = this.selectObjMap.map;
		var selectTreeArr = new Array();
		for(var key in map){
			if(map[key]!=null && key!="extend"){
				var treeValueArr = map[key].parentNode.value.split(";");
				var tempMap = new common.Map();
				tempMap.put("id",treeValueArr[0]);
				tempMap.put("parentId",treeValueArr[1]);
				tempMap.put("name",treeValueArr[2]);
				tempMap.put("type",treeValueArr[3]);
				tempMap.put("level",treeValueArr[4]);
				tempMap.put("path",treeValueArr[5]);
				selectTreeArr.push(tempMap);
			}
		}
		return selectTreeArr;
	},
	getSelectTreeMap2:function(){
		var tempMap = new common.Map();
		var map = this.selectObjMap.map;
		
		var ids = "";
		var names = "";
		
		for(var key in map){
			if(map[key]!=null && key!="extend"){
				var treeValueArr = map[key].parentNode.value.split(";");
				ids +=treeValueArr[0] + ",";
				names += treeValueArr[2] + ",";
			}
		}
		tempMap.put("id",ids.substr(0,ids.length-1));
		tempMap.put("name",names.substr(0,names.length-1));
		return tempMap;
	},
	showAll:function() {//显示所有
		this.changeAllState(true);
	},
	hideAll:function(){//隐藏所有
		this.changeAllState(false);
	},
	changeAllState:function(isOpen) {
		for(var i=0;i<this.childDivIdArr.length;i++){
			var divId = this.childDivIdArr[i];
			var childDiv = document.getElementById(divId);
			if(childDiv!=null){
				if( (childDiv.isOpen == false || childDiv.isOpen == 'false') && isOpen ){//隐藏就显示
					this.changeState(divId,true);
				}else if( (childDiv.isOpen == true || childDiv.isOpen == 'true') && !isOpen ){//显示就隐藏
					this.changeState(divId,false);
				}
			}
		}
	},
	//显示或隐藏所有
	changeAllState2:function() {
		this.changeAllState(this.isOpen);
		if(this.isOpen) {
			document.getElementById("expendId").value = "收起";
		}else{
			document.getElementById("expendId").value = "展开";
		}
		this.isOpen = !this.isOpen;
	},
	/**
	 * 获取父窗口对象
	 */
	getParentObj:function(){
		var arr=window.dialogArguments;
		if(arr){
			return arr[0];
		}else{
			return opener;
		}
		
		//return (window.dialogArguments) ? window.dialogArguments : opener;
	},
	//树型选择框中的按钮功能开始
	doOK:function(){
		var parent = this.getParentObj();
		var selectTreeArr = this.getSelectTreeMap();
		var selectTreeMap = this.getSelectTreeMap2();
		if(selectTreeMap.get("id")==null || selectTreeMap.get("id")==""){//单选
			selectTreeMap = this.objMap;
		}
		try{
			eval("parent."+this.treeObj+".idObj.value=selectTreeMap.get('id');");
			eval("parent."+this.treeObj+".viewObj.value=selectTreeMap.get('name');");
		}catch(e){
			window.close();
		}
		try{	
			//eval("parent."+this.treeObj+"_doAfterSelect(selectTreeArr,selectTreeMap)");
			eval("parent."+this.treeObj+"_doAfterSelect(selectTreeMap)");
			window.close();
		}catch(e){
			//alert("父页面没有函数:"+this.treeObj+"_doAfterSelect(selectTreeArr,selectTreeMap) 或调用该函数出错");
			window.close();
		}
	},
	//重置:1.清空父窗口的值,2.取消所有选择,3.清空搜索条件,4.将数据还原为没有搜索条件的情况
	doClear:function(){
		//this.formUtil.disButton(false);
		var parent = this.getParentObj();
		try{
			eval("parent."+this.treeObj+".idObj.value='';");
			eval("parent."+this.treeObj+".viewObj.value='';");
		}catch(e){
			
		}
		try{
			eval("parent."+this.treeObj+"_doClear()");//清除父窗口的值
		}catch(e){
			//alert("父页面没有函数:"+this.treeObj+"_doClear() 或调用该函数出错");
		}
		
		if(document.getElementsByName("searchName")){//清空搜索条件
			var searchObj = document.getElementsByName("searchName")[0];
			if(searchObj!=null){
				searchObj.value = "";
				this.doSearch();
			}
		}
		//window.close();
	},
	//刷新
	doRefresh:function(){
		//this.formUtil.disButton(false);
		//action=doRefresh
		var searchName = document.getElementsByName("searchName")[0].value;
		var url = this.action + "&isRefresh=true&condition="+this.condition+this.paras + "&searchName="+searchName;
		this.xmlHttp.sendData(url,this.xmlHttp.SEND_GET,"refreshTree");//get方式
	},
	//搜索
	doSearch:function(){
		//this.formUtil.disButton(false);
		
		//action=doSearch
		var searchName = document.getElementsByName("searchName")[0].value.trim();
		if(searchName!="") this.isOpen = false;
		var url = this.action + "&action=doSearch&isSearchAble=true&condition="+this.condition+this.paras + "&searchName="+searchName;
		this.xmlHttp.sendData(url,this.xmlHttp.SEND_GET,"refreshTree");//get方式
		//this.progressBarUtil.start();
	},
	//输入搜索条件后回车就进行搜索
	toSearch:function(){
		if(event && event.keyCode=="13"){
			if(document.getElementById("search")){
				document.getElementById("search").click();
			}
		}
	}
	//树型选择框中的按钮功能结束
}

function refreshTree(result){
	//document.getElementById("treeDivId").innerHTML = "";
	document.getElementById("treeDivId").innerHTML = result;
	if(document.getElementsByName("searchName")){
		if(document.getElementsByName("searchName")[0].value.trim()!=""){
			document.getElementById("expendId").value = "收起";
		}
	}
	//this.progressBarUtil.stop();
	
	var formUtil = common.ObjectFactory.getInstance().getFormUtil();
	formUtil.replayDisButton();
}

